package com.ruoyi.system.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ReportAssessTableMapper;
import com.ruoyi.system.domain.ReportAssessTable;
import com.ruoyi.system.service.IReportAssessTableService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-29
 */
@Service
public class ReportAssessTableServiceImpl implements IReportAssessTableService {
    @Autowired
    private ReportAssessTableMapper reportAssessTableMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ReportAssessTable selectReportAssessTableById(Long id) {
        return reportAssessTableMapper.selectReportAssessTableById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param reportAssessTable 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ReportAssessTable> selectReportAssessTableList(ReportAssessTable reportAssessTable) {
        return reportAssessTableMapper.selectReportAssessTableList(reportAssessTable);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param reportAssessTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertReportAssessTable(ReportAssessTable reportAssessTable) {
        return reportAssessTableMapper.insertReportAssessTable(reportAssessTable);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param reportAssessTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateReportAssessTable(ReportAssessTable reportAssessTable) {
        return reportAssessTableMapper.updateReportAssessTable(reportAssessTable);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteReportAssessTableByIds(Long[] ids) {
        return reportAssessTableMapper.deleteReportAssessTableByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteReportAssessTableById(Long id) {
        return reportAssessTableMapper.deleteReportAssessTableById(id);
    }

    @Override
    public int insertReportAssessTableList(List<ReportAssessTable> allList) {
        return reportAssessTableMapper.insertReportAssessTableList(allList);
    }

    @Override
    public List<Map<String, Object>> countCharByUser(int countUser, List<Long> deptIds, Long taskId) {
        List<Map<String, Object>> maps = reportAssessTableMapper.countCharByUser(deptIds, taskId);
        //消极指标
        List<String> xiaojiArray = Arrays.asList("重度", "中度");
        List<Map<String, Object>> xiaojiList = reportAssessTableMapper.countCharByUserLevel(xiaojiArray, deptIds, taskId, 2);
        //积极指标
        //如果是积极指标，转换和消极指标对应等级
        //（很高、较高=正常，较低=轻度，极低=中度）
        List<String> jijiArray = Arrays.asList("中度");
        List<Map<String, Object>> jijiList = reportAssessTableMapper.countCharByUserLevel(jijiArray, deptIds, taskId, 1);


        List<Map<String, Object>> results = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String assess_table_name = String.valueOf(map.get("assess_table_name"));
            String assess_table_type = String.valueOf(map.get("assess_table_type"));
            String assess_table_id = String.valueOf(map.get("assess_table_id"));

            Long count = 0L;
            //2 消极/ 1积极
            if ("2".equals(assess_table_type)) {
                count = getCountLevel(assess_table_name, xiaojiList);
            } else if ("1".equals(assess_table_type)) {
                count = getCountLevel(assess_table_name, jijiList);
            }

            Map<String, Object> result = new HashMap<>();
            result.put("name", assess_table_name);
            result.put("tableId", assess_table_id);
            result.put("value", count);
            double percentage = ((double) count / countUser) * 100;
            result.put("percentage", String.format("%.2f%%", percentage));
            results.add(result);
        }
        if(!results.isEmpty()){
            results.sort((a, b) -> {
                String percentageA = (String) a.get("percentage");
                String percentageB = (String) b.get("percentage");

                // 去掉百分号并转换为 double 类型
                double valueA = Double.parseDouble(percentageA.replace("%", ""));
                double valueB = Double.parseDouble(percentageB.replace("%", ""));

                // 倒序排列（从高到低）
                return Double.compare(valueB, valueA);
            });
        }
        return results;
    }

    private Long getCountLevel(String assessTableName, List<Map<String, Object>> xiaojiList) {
        for (Map<String, Object> map : xiaojiList) {
            String assess_table_name = String.valueOf(map.get("name"));
            if (assess_table_name.equals(assessTableName)) {
                return (Long) map.get("value");
            }
        }
        return 0L;
    }

    @Override
    public List<Map<String, Object>> countTaskLevelCharByUser(List<Long> deptIds, List<String> nameList, Long taskId, Long tableId) {
        return reportAssessTableMapper.countTaskLevelCharByUser(deptIds, nameList, taskId, tableId);
    }
}
